package cn.icanci.loopstack.ras.client.aop;

import cn.icanci.loopstack.ras.client.cache.loadbalancing.LoadBalancingSupport;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * AOP拦截 - 拦截执行时间和相关请求参数，记录到缓存
 * 
 * @author icanci
 * @since 1.0 Created in 2023/01/16 19:56
 */
@Aspect
@Component
public class RpcCallAop {

    @Pointcut("execution(public * cn.icanci.loopstack.ras.client.facade.RpcCallFacade.reallyCall(..))")
    private void callAop() {

    }

    @Around("callAop()")
    public Object doBefore(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        long startTime = System.currentTimeMillis();
        Object returnVal = pjp.proceed();
        long endTime = System.currentTimeMillis();
        // 刷新数据 
        LoadBalancingSupport.refreshCallSpeedInfo(args, (endTime - startTime));
        return returnVal;
    }
}
